define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var ajax = require("resource/js/component/ajax.js");
	var urlTool = require("resource/js/component/urlTool.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var alertMsg = require("resource/js/component/alerts.js");
	var CONST = require('oa/act/js/actcont.js');

	var group={},dgutil;

	var initPage = function(url){
		initDrawer();
		if(group && group.groupId){
			ajax.init().success(function(code,msg,data){
				if(data && data.length>0){
					$('#groupName').textbox('setValue',data[0].groupName);
					$('#groupDes').textbox('setValue',data[0].groupDes);
					if(data[0].processModel){
						$.each(data[0].processModel,function(i,item){
							addProcess(item);
						});
					}
				}
			}).post(BASEPATH+'/oa/act/processGroup/queryGroupsAndProcess.htm',{groupId:group.groupId});
		}
		//展开流程列表
		$('#addProcess').bind('click');
		$('#addProcess').click(function(){
			$(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
		});
		//保存按钮
		$('#btn_submit').bind('click');
		$('#btn_submit').click(function(){
			saveGroup(url);
		});
	}
	/**
	 * 添加/移除流程事件
	 */
	var addProcess = function(process){
		var checkeds = getCheckeds();
		var isExist = $.inArray(process.processKey,checkeds) > -1;
		if(isExist){
			return;
		}else{
			$('<li class="warp-item process" style="position: relative;">'
					+'<img style="position: relative;top:20px;" src="'+BASEPATH+process.iconUrl+'" width="46" />'
					+" <div class='closeTarget'></div>"
					+'<a href="javascript:void(0)">'+process.businessName+'</a>'+'</li>')
				.prependTo($('#processWarp')).data('key',process.processKey)
				.find('.closeTarget').click(function(){
					$(this).parent().remove();
				});
		}
	}
	/**
	 * 右侧弹出列表初始化
	 */
	var initDrawer = function(){
		$('#moudelType').val(CONST.ACT_MOUDEL_TYPE.APPROVAL);
		var columns=[[
		              {field:'business_name',title:'流程名称',width:100,align:'center',height:50,formatter:function(value,row,index){
		        				return "<xmp>"+value+"</xmp>";
		            	}},
		            ]];
    	var toolbar = {title:{
    		text:"我的流程"
    	}}
    	var datagridconfig = {
    			fit: true,
    			fitColumns:true,
                rownumbers:true,
    			pagination:false,
    			singleSelect:false,
    			url:BASEPATH+"/oa/act/approval/progress/queryProgress.htm",
    			//queryParams:{moudelType:CONST.ACT_MOUDEL_TYPE.APPROVAL},
    			toolbar:toolbar,
    			columns:columns,
    			eid:"processlist",
    			args:"searchform",
    	};
    	using(["datagrid","parser"],function(){
    		dgutil = new EUtil.ajaxDataGrid(datagridconfig);
    	});
		$(".jgui-drawer-right").find(".close").click(function() {
			$(".jgui-drawer-right").addClass("jgui-drawer-close").removeClass("jgui-drawer-open");
		});
		$('#add').click(function(){
		 var checks = $("#processlist").datagrid("getChecked");
		 $.each(checks,function(i,item){
			 item.businessName = item.business_name;
			 item.iconUrl = item.icon_url;
			 item.processKey = item.process_key;
			 addProcess(item);
		 });
		});
	}
	/**
	 * 获取选中流程key数组
	 */
	var getCheckeds = function(){
		var checkeds = [];
		$.each($('.process'),function(i,item){
			checkeds.push($(item).data('key'));
		});
		return checkeds;
	}
	/**
	 * 保存提交
	 */
	var saveGroup = function(url){
		if($('#editForm').form('validate')){
			group.groupName = $('#groupName').textbox('getValue');
			group.groupDes = $('#groupDes').textbox('getValue');
			group.checkeds = getCheckeds();
			window.parent.parent.$(".oa-maskAll").show();
			ajax.init().success(function(code,msg,data){
				if(code==0){
					$('#btn_submit').unbind('click');
					window.parent.parent.$(".oa-maskAll").hide();
					alertMsg.show(msg,function(){
						 window.parent.$('.dlg-goback').click();
					});

				}
			})
			.error(function(code,msg,data){
				window.parent.parent.$(".oa-maskAll").hide();
				alertMsg.show(msg);
			})
			.contentType('application/json').post(BASEPATH+url,JSON.stringify(group));
		}
	}
	module.exports={
		init:function(){
			var url;
			var groupId=urlTool.getParam("groupId");
			if(groupId){
				url = '/oa/act/processGroup/updateDefinedGroup.htm'
				group.groupId=groupId
			}else{
				url= '/oa/act/processGroup/saveDefinedGroup.htm'
			}
			using(["parser","datagrid", "textbox","linkbutton"], function() {
				initPage(url);
			});
		}
	}
});
